﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace JobCreateInDocs2_WinForms_Probe
{
    public static class SPSecurityHelper
    {
        #region Group
        public static bool ExistsGroupWeb(string parSiteUrl, String mName)
        {
            Boolean mRet = false;
            using (SPSite Site = new SPSite(parSiteUrl))
            {
                mRet = ExistsGroupWeb(Site, mName);
            }
            return mRet;
        }
        public static bool ExistsGroupWeb(SPSite Site, String mName)
        {
            Boolean mRet = false;
            using (SPWeb mWeb = Site.OpenWeb())
            {
                mRet = ExistsGroupWeb(mWeb, mName);
            }
            return mRet;
        }
        public static bool ExistsGroupWeb(SPWeb mWeb, String mName)
        {
            Boolean mRet = false;
            foreach (SPGroup mGroup in mWeb.SiteGroups)
            {
                if (mGroup.Name == mName)
                {
                    mRet = true;
                    break;
                }
            }
            return mRet;
        }
        public static SPGroup GetWeGroup(string parSiteUrl, String mName)
        {
            SPGroup mRet = null;
            using (SPSite Site = new SPSite(parSiteUrl))
            {
                mRet = GetWeGroup(Site, mName);
            }
            return mRet;
        }
        public static SPGroup GetWeGroup(SPSite Site, String mName)
        {
            SPGroup mRet = null;
            using (SPWeb mWeb = Site.OpenWeb())
            {
                mRet = GetWeGroup(mWeb, mName);
            }
            return mRet;
        }
        public static SPGroup GetWeGroup(SPWeb mWeb, String mName)
        {
            SPGroup mRet = null;
            if (ExistsGroupWeb(mWeb, mName))
            {
                mRet = mWeb.SiteGroups[mName];
            }
            else
            {
                //SPUser oUser = mWeb.AllUsers["DOMAIN\\User"];
                //mWeb.SiteGroups.Add(mName, oUser, oUser, mName);
                mWeb.SiteGroups.Add(mName, mWeb.SiteAdministrators[0], null, mName);
                if (ExistsGroupWeb(mWeb, mName))
                    mRet = mWeb.SiteGroups[mName];
            }
            return mRet;
        }
        public static string GetNameGroupByString(string GroupValue)
        {
            string Ret = null;
            if (GroupValue != null)
            {
                int mPos = GroupValue.IndexOf(";#");
                if (mPos > 0)
                {
                    Ret = GroupValue.Substring(GroupValue.IndexOf(";#") + 2);  // GroupValue.Length - 2 - GroupValue.IndexOf(";#")
                    Ret = Ret.Trim();
                }
            }
            return Ret;
        }
        #endregion Group
    }
}
